﻿using Exceptionless.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastConsole;
using static FastConsole.Out;
using static FastConsole.FColor;
using Abp.Settings;
using Exceptionless;

namespace Abp.Logging
{
    internal class ConsoleLogger : IExceptionlessLog
    {
        public ConsoleLogger()
        {
            var client = ExceptionlessClient.Default;

            string value = client.Configuration.Settings.GetString(ExceptionlessSettingProvider.ConsoleLogLevel, "Warn");
            MinimumLogLevel = LogLevel.FromString(value);
        }


        public LogLevel MinimumLogLevel { get; set; }

        bool EnabledTrace => MinimumLogLevel != LogLevel.Off && MinimumLogLevel == LogLevel.Trace;
        bool EnabledDebug => MinimumLogLevel != LogLevel.Off && (EnabledTrace || MinimumLogLevel == LogLevel.Debug);
        bool EnabledInfo => MinimumLogLevel != LogLevel.Off && (EnabledDebug || MinimumLogLevel == LogLevel.Info);
        bool EnabledWarn => MinimumLogLevel != LogLevel.Off && (EnabledInfo || MinimumLogLevel == LogLevel.Warn);
        bool EnabledError => MinimumLogLevel != LogLevel.Off && (EnabledWarn || MinimumLogLevel == LogLevel.Error);

        public void Debug(string message, string source = null)
        {
            if (EnabledDebug)
            {
                Print("[DEBUG]", Blue);
                if (source == null)
                    Println(message, Blue);
                else
                    Println($"[{source}]{message}", Blue);
                //LoggerProvider.CreateLogger(source).Log<string>(Microsoft.Extensions.Logging.LogLevel.Debug, new Microsoft.Extensions.Logging.EventId (0), message, null, null);
            }
        }

        public void Error(string message, string source = null, Exception exception = null)
        {
            if (EnabledError)
            {
                Print("[ERROR]", Red);
                if (source == null)
                {
                    Println(message, White);
                }
                else
                {
                    Println($"[{source}]{message}", White);
                }
                while (exception != null)
                {
                    Println(exception.Message, White);
                    Println();
                    Println(exception.StackTrace, White);
                    Println();
                }
                //LoggerProvider.CreateLogger(source).Log<string>(Microsoft.Extensions.Logging.LogLevel.Error, new Microsoft.Extensions.Logging.EventId(0), message, exception, null);
            }

        }

        public void Flush()
        {

        }

        public void Info(string message, string source = null)
        {
            if (EnabledInfo)
            {
                Print("[INFO]", Green);
                if (source == null)
                    Println(message, DarkGray);
                else
                    Println($"[{source}]{message}", DarkGray);
                //LoggerProvider.CreateLogger(source).Log<string>(Microsoft.Extensions.Logging.LogLevel.Information, new Microsoft.Extensions.Logging.EventId(0), message, null, null);
            }

        }

        public void Trace(string message, string source = null)
        {
            if (EnabledTrace)
            {
                Print("[TRACE]", DarkGreen);
                if (source == null)
                    Println(message, DarkGray);
                else
                    Println($"[{source}]{message}", DarkGray);
                //LoggerProvider.CreateLogger(source).Log<string>(Microsoft.Extensions.Logging.LogLevel.Trace, new Microsoft.Extensions.Logging.EventId(0), message, null, null);
            }

        }

        public void Warn(string message, string source = null)
        {
            if (EnabledWarn)
            {
                Print("[WARN]", Yellow);
                if (source == null)
                    Println(message, White);
                else
                    Println($"[{source}]{message}", White);
                //LoggerProvider.CreateLogger(source).Log<string>(Microsoft.Extensions.Logging.LogLevel.Warning, new Microsoft.Extensions.Logging.EventId(0), message, null, null);
            }

        }
    }
}
